package com.superMarket.project.settlement;

import com.superMarket.project.business.BuyGoodsService;
import com.superMarket.project.domain.ConsumptionReduction;
import com.superMarket.project.domain.Customer;
import com.superMarket.project.domain.Fruit;
import com.superMarket.project.domain.ShoppingRecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品结算类
 * @author mrguo
 * @date 2024/11/04
 */
public class GoodsSettlement {

    /**
     * 计算并返回顾客购买商品总价
     * @param salesScenario
     * @param customer
     * @param recordList
     * @param reduction
     */
    private static void calculateTotalPriceOfGoods(String salesScenario, Customer customer, List<ShoppingRecord> recordList, ConsumptionReduction reduction){
        // 获取顾客A买商品场景下的水果集合数据
        List<Fruit> fruitList = BuyGoodsService.getFruitList(salesScenario);
        Map<String, Double> priceMap = BuyGoodsService.calculateTotalPriceOfGoods(customer, fruitList, recordList, reduction);

        // 定义原购物总价
        double originalTotalPrice = priceMap.get("originalTotalPrice");
        // 定义减免后的购物总价
        double totalPrice = priceMap.get("totalPrice");

        String scenarioName = "";
        if("1".equals(salesScenario)){
            scenarioName = "顾客A购物场景";
        }else if("2".equals(salesScenario)){
            scenarioName = "顾客B购物场景";
        }else if("3".equals(salesScenario)){
            scenarioName = "顾客C购物场景";
        }else if("4".equals(salesScenario)){
            scenarioName = "顾客D购物场景";
        }

        // 水果单价等数据
        StringBuilder fruitSb = new StringBuilder();
        for(Fruit fruit: fruitList){
            fruitSb.append(fruit.getName() + "(" + fruit.getPrice() + "元/斤，" +"折扣为" +fruit.getDiscount()+ ")，");
        }
        if(reduction != null){
            fruitSb.append("购物满" + reduction.getReduceStandard() + "元减免" + reduction.getReduceAmount() + "元");
        }else {
            fruitSb.delete(fruitSb.length() - 1, fruitSb.length());
        }

        // 购买记录数据
        StringBuilder recordSb = new StringBuilder();
        for(ShoppingRecord record: recordList){
            recordSb.append(record.getBuyQuantity() + "斤" + record.getFruitName() +"、");
        }
        recordSb.delete(recordSb.length() - 1, recordSb.length());

        System.out.println(scenarioName + ": ");
        System.out.println(fruitSb.toString());

        // 如果原总价跟减免后的总价不同，同时输出
        if(originalTotalPrice == totalPrice){
            System.out.println(customer.getName() + "购买了" + recordSb.toString() + "，总价为" + totalPrice + "元\n");
        }else{
            System.out.println(customer.getName() + "购买了" + recordSb.toString()+ "，原总价为" + originalTotalPrice + "元，减免后总价为" + totalPrice + "元\n");
        }
    }

    public static void main(String[] args) {
        // 定义顾客A
        Customer customerA = new Customer("顾客A");
        // 购买场景1购买记录数据
        List<ShoppingRecord> record1List = new ArrayList<>();
        record1List.add(new ShoppingRecord("顾客A", "苹果", 2));
        record1List.add(new ShoppingRecord("顾客A", "草莓", 1));
        // 计算购物总价
        calculateTotalPriceOfGoods("1", customerA, record1List, null);

        // 定义顾客B
        Customer customerB = new Customer("顾客B");
        // 购买场景2购买记录数据
        List<ShoppingRecord> record2List = new ArrayList<>();
        record2List.add(new ShoppingRecord("顾客B", "苹果", 2));
        record2List.add(new ShoppingRecord("顾客B", "草莓", 2));
        record2List.add(new ShoppingRecord("顾客B", "芒果", 1));
        // 计算购物总价
        calculateTotalPriceOfGoods("2", customerB, record2List, null);

        // 定义顾客C
        Customer customerC = new Customer("顾客C");
        // 购买场景3购买记录数据
        List<ShoppingRecord> record3List = new ArrayList<>();
        record3List.add(new ShoppingRecord("顾客C", "苹果", 2));
        record3List.add(new ShoppingRecord("顾客C", "草莓", 1));
        record3List.add(new ShoppingRecord("顾客C", "芒果", 2));
        // 计算购物总价
        calculateTotalPriceOfGoods("3", customerC, record3List, null);

        // 定义顾客D
        Customer customerD = new Customer("顾客D");
        // 购买场景4购买记录数据
        List<ShoppingRecord> record4List = new ArrayList<>();
        record4List.add(new ShoppingRecord("顾客D", "苹果", 6));
        record4List.add(new ShoppingRecord("顾客D", "草莓", 3));
        record4List.add(new ShoppingRecord("顾客D", "芒果", 7));
        // 计算购物总价
        calculateTotalPriceOfGoods("4", customerD, record4List, new ConsumptionReduction(100d, 10d));
    }
}
